import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class StudentDatabase {
    private static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/java";
    private static final String JDBC_USER = "root";
    private static final String JDBC_PASSWORD = "123456";


    // 增加学生记录
    public static void addStudent(String no, String name, String phone) {
        String sql = "INSERT INTO Student (no, name, phone) VALUES (?,?,?)";
        try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, no);
            pstmt.setString(2, name);
            pstmt.setString(3, phone);
            pstmt.executeUpdate();
            System.out.println("学生记录添加成功");
        } catch (SQLException e) {
            System.out.println("添加学生记录时出错: " + e.getMessage());
        }
    }


    // 删除学生记录
    public static void deleteStudent(String no) {
        String sql = "DELETE FROM Student WHERE no =?";
        try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, no);
            int rows = pstmt.executeUpdate();
            if (rows > 0) {
                System.out.println("学生记录删除成功");
            } else {
                System.out.println("未找到该学号的学生记录");
            }
        } catch (SQLException e) {
            System.out.println("删除学生记录时出错: " + e.getMessage());
        }
    }


    // 修改学生记录
    public static void updateStudent(String no, String name, String phone) {
        String sql = "UPDATE Student SET name =?, phone =? WHERE no =?";
        try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, name);
            pstmt.setString(2, phone);
            pstmt.setString(3, no);
            int rows = pstmt.executeUpdate();
            if (rows > 0) {
                System.out.println("学生记录更新成功");
            } else {
                System.out.println("未找到该学号的学生记录");
            }
        } catch (SQLException e) {
            System.out.println("更新学生记录时出错: " + e.getMessage());
        }
    }


    // 查询学生记录
    public static void queryStudent(String no) {
        String sql = "SELECT * FROM Student WHERE no =?";
        try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql);) {
            pstmt.setString(1, no);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                System.out.println("学号: " + rs.getString("no"));
                System.out.println("姓名: " + rs.getString("name"));
                System.out.println("电话: " + rs.getString("phone"));
            } else {
                System.out.println("未找到该学号的学生记录");
            }
        } catch (SQLException e) {
            System.out.println("查询学生记录时出错: " + e.getMessage());
        }
    }


    public static void main(String[] args) {
        // 示例调用
        addStudent("1001", "张三", "123456789");
        queryStudent("1001");
        updateStudent("1001", "李四", "987654321");
        queryStudent("1001");
        deleteStudent("1001");
        queryStudent("1001");
    }
}
